<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="合同编号" prop="contractCode">
        <el-input v-model="queryParams.contractCode" placeholder="请输入合同编号" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="合同名称" prop="contractName">
        <el-input v-model="queryParams.contractName" placeholder="请输入合同名称" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="合同审核状态" prop="contractStatus">
        <el-select v-model="queryParams.contractStatus" placeholder="请选择合同审核状态" clearable>
          <el-option v-for="dict in dict.type.com_audit_contract_status" :key="dict.value" :label="dict.label"
                     :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item label="客户名称" prop="companyName">
        <el-input v-model="queryParams.companyName" placeholder="请输入客户名称" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="联系人名称" prop="companyUserName">
        <el-input v-model="queryParams.companyUserName" placeholder="请输入联系人名称" clearable
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="联系人手机" prop="mobile">
        <el-input v-model="queryParams.mobile" placeholder="请输入联系人手机" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                   v-hasPermi="['customer:contract:add']">新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
                   v-hasPermi="['customer:contract:remove']">批量删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="info" plain icon="el-icon-upload2" size="mini" @click="handleImport"
                   v-hasPermi="['customer:contract:import']">导入
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                   v-hasPermi="['customer:contract:export']">导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="contractList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="45" align="center"/>
      <el-table-column label="合同编号" align="center" prop="contractCode"/>
      <el-table-column label="合同名称" align="center" prop="contractName"/>
      <el-table-column label="客户名称" align="center" prop="companyName" width="200"/>
      <el-table-column label="客户合同状态" align="center" prop="companyStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.com_company_contract_type" :value="scope.row.companyStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="合同开始时间" align="center" prop="contractStartDate" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.contractStartDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="合同结束时间" align="center" prop="contractEndDate" width="100">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.contractEndDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="合同金额" align="center" prop="contractAmount" width="80"/>
      <el-table-column label="合同状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_order_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="合同审核状态" align="center" prop="contractStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.com_audit_contract_status" :value="scope.row.contractStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="260">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-upload2" @click="handleFileUpload(scope.row)"
                     v-hasPermi="['customer:contract:fileUpload']">上传
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-download" @click="handleFileExport(scope.row)"
                     v-hasPermi="['customer:contract:fileExport']">下载
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-view" @click="handleView(scope.row, scope.index)"
                     v-hasPermi="['customer:contract:query']">详情
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['customer:contract:edit']">修改
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['customer:contract:remove']">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>

    <!-- 新增合同管理对话框 -->
    <el-dialog :title="title" :visible.sync="addopen" width="850px" append-to-body>
      <el-form :inline="true" class="demo-form-inline" ref="form" :model="form" :rules="rules" label-width="150px">
        <el-form-item label="合同名称" prop="contractName">
          <el-input v-model="form.contractName" placeholder="请输入合同名称"/>
        </el-form-item>
        <el-form-item label="合同金额" prop="contractAmount">
          <el-input v-model="form.contractAmount" placeholder="请输入合同金额"/>
        </el-form-item>
        <el-form-item label="合同开始时间" prop="contractStartDate">
          <el-date-picker clearable v-model="form.contractStartDate" type="date" value-format="yyyy-MM-dd"
                          placeholder="请选择合同开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="合同结束时间" prop="contractEndDate">
          <el-date-picker clearable v-model="form.contractEndDate" type="date" value-format="yyyy-MM-dd"
                          placeholder="请选择合同结束时间">
          </el-date-picker>
        </el-form-item>
        <el-row>
          <div class="paddingDistance">
            <el-form-item>
              <el-button size="mini" @click="showCustomerDrawer">选择客户</el-button>
            </el-form-item>
          </div>
          <el-form-item label="客户编码" prop="companyCode">
            <el-input v-model="form.companyCode" placeholder="请输入客户编码"/>
          </el-form-item>
          <el-form-item label="客户名称" prop="companyName">
            <el-input v-model="form.companyName" placeholder="请输入客户名称"/>
          </el-form-item>
          <el-form-item label="联系人名称" prop="companyUserName">
            <el-input v-model="form.companyUserName" placeholder="请输入联系人名称"/>
          </el-form-item>
          <el-form-item label="联系人手机" prop="mobile">
            <el-input v-model="form.mobile" placeholder="请输入联系人手机"/>
          </el-form-item>
        </el-row>

        <el-row>
          <div class="paddingDistance">
            <el-form-item>
              <el-button size="mini" @click="showProductDrawer">选择产品</el-button>
            </el-form-item>
          </div>
          <el-form-item label="产品编号" prop="productCode">
            <el-input v-model="form.productCode" placeholder="请输入产品编号"/>
          </el-form-item>
          <el-form-item label="产品名称" prop="productName">
            <el-input v-model="form.productName" placeholder="请输入产品名称"/>
          </el-form-item>
        </el-row>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 修改合同管理对话框 -->
    <el-dialog :title="title" :visible.sync="updateopen" width="850px" append-to-body>
      <el-form :inline="true" class="demo-form-inline" ref="form" :model="form" :rules="rules" label-width="150px">
        <el-form-item label="合同编号" prop="contractCode">
          <el-input v-model="form.contractCode" placeholder="请输入合同编号"/>
        </el-form-item>
        <el-form-item label="合同名称" prop="contractName">
          <el-input v-model="form.contractName" placeholder="请输入合同名称"/>
        </el-form-item>
        <el-form-item label="合同开始时间" prop="contractStartDate">
          <el-date-picker clearable v-model="form.contractStartDate" type="date" value-format="yyyy-MM-dd"
                          placeholder="请选择合同开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="合同结束时间" prop="contractEndDate">
          <el-date-picker clearable v-model="form.contractEndDate" type="date" value-format="yyyy-MM-dd"
                          placeholder="请选择合同结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="合同金额" prop="contractAmount">
          <el-input v-model="form.contractAmount" placeholder="请输入合同金额"/>
        </el-form-item>

        <el-form-item label="合同状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio v-for="dict in dict.type.sys_order_status" :key="dict.value" :label="dict.value">{{
                dict.label
              }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="合同审核状态" prop="contractStatus">
          <el-radio-group v-model="form.contractStatus">
            <el-radio v-for="dict in dict.type.com_audit_contract_status" :key="dict.value" :label="dict.value">{{
                dict.label
              }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="拓展字段" prop="ext">
          <el-input v-model="form.ext" type="textarea" placeholder="请输入内容"/>
        </el-form-item>

        <el-row>
          <el-form-item>
            <el-button size="mini" @click="showCustomerDrawer">选择客户</el-button>
          </el-form-item>
          <el-form-item label="客户id" prop="companyId">
            <el-input v-model="form.companyId" placeholder="请输入客户id"/>
          </el-form-item>
          <el-form-item label="客户编码" prop="companyCode">
            <el-input v-model="form.companyCode" placeholder="请输入客户编码"/>
          </el-form-item>
          <el-form-item label="客户名称" prop="companyName">
            <el-input v-model="form.companyName" placeholder="请输入客户名称"/>
          </el-form-item>
          <el-form-item label="状态" prop="companyStatus">
            <el-radio-group v-model="form.companyStatus">
              <el-radio v-for="dict in dict.type.com_company_contract_type" :key="dict.value"
                        :label="parseInt(dict.value)">{{ dict.label }}
              </el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="联系人名称" prop="companyUserName">
            <el-input v-model="form.companyUserName" placeholder="请输入联系人名称"/>
          </el-form-item>
          <el-form-item label="联系人职务" prop="companyTitle">
            <el-input v-model="form.companyTitle" placeholder="请输入联系人职务"/>
          </el-form-item>
          <el-form-item label="联系人手机" prop="mobile">
            <el-input v-model="form.mobile" placeholder="请输入联系人手机"/>
          </el-form-item>
        </el-row>

        <el-row>
          <el-form-item>
            <el-button size="mini" @click="showProductDrawer">选择产品</el-button>
          </el-form-item>
          <el-form-item label="产品ID" prop="productId">
            <el-input v-model="form.productId" placeholder="请输入产品ID"/>
          </el-form-item>
          <el-form-item label="产品编号" prop="productCode">
            <el-input v-model="form.productCode" placeholder="请输入产品编号"/>
          </el-form-item>
          <el-form-item label="产品名称" prop="productName">
            <el-input v-model="form.productName" placeholder="请输入产品名称"/>
          </el-form-item>
        </el-row>
        <!-- <el-form-item label="删除标志" prop="delFlag">
          <el-radio-group v-model="form.delFlag">
            <el-radio v-for="dict in dict.type.sys_delete_status" :key="dict.value" :label="dict.value">{{ dict.label
            }}</el-radio>
          </el-radio-group>
        </el-form-item> -->
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 合同管理详情对话框 -->
    <el-dialog :title="title" :visible.sync="detailopen" width="850px" append-to-body>
      <el-form :inline="true" class="demo-form-inline" ref="form" :model="form" :rules="rules" label-width="150px">
        <el-form-item label="合同编号" prop="contractCode">
          <el-input v-model="form.contractCode" placeholder="请输入合同编号"/>
        </el-form-item>
        <el-form-item label="合同名称" prop="contractName">
          <el-input v-model="form.contractName" placeholder="请输入合同名称"/>
        </el-form-item>
        <el-form-item label="合同开始时间" prop="contractStartDate">
          <el-date-picker clearable v-model="form.contractStartDate" type="date" value-format="yyyy-MM-dd"
                          placeholder="请选择合同开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="合同结束时间" prop="contractEndDate">
          <el-date-picker clearable v-model="form.contractEndDate" type="date" value-format="yyyy-MM-dd"
                          placeholder="请选择合同结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="合同金额" prop="contractAmount">
          <el-input v-model="form.contractAmount" placeholder="请输入合同金额"/>
        </el-form-item>
        <el-form-item label="客户id" prop="companyId">
          <el-input v-model="form.companyId" placeholder="请输入客户id"/>
        </el-form-item>
        <el-form-item label="客户编码" prop="companyCode">
          <el-input v-model="form.companyCode" placeholder="请输入客户编码"/>
        </el-form-item>
        <el-form-item label="客户名称" prop="companyName">
          <el-input v-model="form.companyName" placeholder="请输入客户名称"/>
        </el-form-item>
        <el-form-item label="状态" prop="companyStatus">
          <el-radio-group v-model="form.companyStatus">
            <el-radio v-for="dict in dict.type.com_company_contract_type" :key="dict.value"
                      :label="parseInt(dict.value)">{{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="联系人名称" prop="companyUserName">
          <el-input v-model="form.companyUserName" placeholder="请输入联系人名称"/>
        </el-form-item>
        <el-form-item label="联系人职务" prop="companyTitle">
          <el-input v-model="form.companyTitle" placeholder="请输入联系人职务"/>
        </el-form-item>
        <el-form-item label="联系人手机" prop="mobile">
          <el-input v-model="form.mobile" placeholder="请输入联系人手机"/>
        </el-form-item>
        <el-form-item label="合同状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio v-for="dict in dict.type.sys_order_status" :key="dict.value" :label="dict.value">{{
                dict.label
              }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="合同审核状态" prop="contractStatus">
          <el-radio-group v-model="form.contractStatus">
            <el-radio v-for="dict in dict.type.com_audit_contract_status" :key="dict.value" :label="dict.value">{{
                dict.label
              }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="产品ID" prop="productId">
          <el-input v-model="form.productId" placeholder="请输入产品ID"/>
        </el-form-item>
        <el-form-item label="产品编号" prop="productCode">
          <el-input v-model="form.productCode" placeholder="请输入产品编号"/>
        </el-form-item>
        <el-form-item label="产品名称" prop="productName">
          <el-input v-model="form.productName" placeholder="请输入产品名称"/>
        </el-form-item>
        <el-form-item label="合同路径" prop="contractPicture">
          <el-input v-model="form.contractPicture" placeholder="请输入合同图片"/>
        </el-form-item>
        <el-form-item label="拓展字段" prop="ext">
          <el-input v-model="form.ext" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="删除标志" prop="delFlag">
          <el-radio-group v-model="form.delFlag">
            <el-radio v-for="dict in dict.type.sys_delete_status" :key="dict.value" :label="dict.value">{{
                dict.label
              }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="detailopen = false">关 闭</el-button>
      </div>
    </el-dialog>

    <!-- 文件上传对话框 -->
    <el-dialog :title="fileUpload.title" :visible.sync="fileUpload.open" width="400px" append-to-body>
      <el-upload ref="upload" :limit="1" accept=".pdf" :headers="fileUpload.headers"
                 :action="fileUpload.url + '?contractId=' + fileUpload.contractId " :disabled="fileUpload.isUploading"
                 :on-progress="handleFileUploadProgress" :on-success="handleFileSuccess" :auto-upload="false" drag>
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <span>单个文件不要超过50兆，仅允许导入PDF格式数据。</span>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">上 传</el-button>
        <el-button @click="fileUpload.open = false">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload ref="upload" :limit="1" accept=".xlsx, .xls" :headers="upload.headers"
                 :action="upload.url + '?updateSupport=' + upload.updateSupport" :disabled="upload.isUploading"
                 :on-progress="handleUploadProgress" :on-success="handleUploadSuccess" :auto-upload="false" drag>
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <div class="el-upload__tip" slot="tip">
            <el-checkbox v-model="upload.updateSupport"/>
            是否更新已经存在数据
          </div>
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;"
                   @click="importTemplate">下载模板
          </el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitUploadForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>

    <!--选择客户抽屉-->
    <el-drawer ref="customerDrawer" size="50%" :visible.sync="isShowCustomerDrawer">
      <sea :isSearch="true" @confirmCustomer="confirmCustomer"></Sea>
    </el-drawer>

    <!--选择产品抽屉-->
    <el-drawer ref="productDrawer" size="50%" :visible.sync="isShowProductDrawer">
      <Product :isSearch="true" @confirmProduct="confirmProduct"></Product>
    </el-drawer>

  </div>
</template>

<script>
import {listContract, getContract, delContract, addContract, updateContract} from "@/api/customer/contract";
import {getToken} from "@/utils/auth";
import Sea from "@/views/customer/sea/index";
import Product from "@/views/customer/product/index";

export default {
  name: "Contract",
  dicts: ['sys_order_status', 'com_company_contract_type', 'sys_delete_status', 'com_audit_contract_status'],
  components: {Sea, Product},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 合同管理表格数据
      contractList: [],
      // 弹出层标题
      title: "",
      // 是否详情显示弹出层
      detailopen: false
      ,
      // 是否新增显示弹出层
      addopen: false
      ,
      // 是否修改显示弹出层
      updateopen: false
      ,
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        id: null,
        contractCode: null,
        contractName: null,
        contractStartDate: null,
        contractEndDate: null,
        contractAmount: null,
        companyId: null,
        companyCode: null,
        companyName: null,
        companyStatus: null,
        companyUserName: null,
        companyTitle: null,
        mobile: null,
        status: null,
        contractStatus: null,
        productId: null,
        productCode: null,
        productName: null,
        contractPicture: null,
        contractAccessory: null,
        ext: null,
      },
      // 表单参数
      form: {},
      // 导入参数
      upload: {
        // 是否显示弹出层
        open: false,
        // 弹出层标题（
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: {Authorization: "Bearer " + getToken()},
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "customer/contract/importData"
      },
      // 表单校验
      rules: {

        mobile: [
          {required: true, message: "不能为空", trigger: "blur"},
          { validator: this.validateMobile, trigger: "blur"}
        ],
        productCode: [
          {required: true, message: "不能为空", trigger: "blur"},
          {max: 50, message: "最多输入50个字", trigger: "blur"}
        ],
        productName: [
          {required: true, message: "不能为空", trigger: "blur"},
          {max: 50, message: "最多输入50个字", trigger: "blur"}
        ],
        contractAmount: [
          {required: true, message: "不能为空", trigger: "blur"},
          {max: 50, message: "最多输入50个字", trigger: "blur"},
          { validator: this.validateMoney, trigger: "blur"}
        ],
        contractStartDate: [
          {required: true, message: "不能为空", trigger: "blur"},
          {max: 50, message: "最多输入50个字", trigger: "blur"}
        ],
        contractEndDate: [
          {required: true, message: "不能为空", trigger: "blur"},
          {max: 50, message: "最多输入50个字", trigger: "blur"}
        ],
        companyName: [
          {required: true, message: "客户名称不能为空", trigger: "blur"},
          {max: 100, message: "最多输入100个字", trigger: "blur"}
        ],
        contractName: [
          {required: true, message: "合同名称不能为空", trigger: "blur"},
          {max: 100, message: "最多输入100个字", trigger: "blur"}
        ]
      },

      // 文件上传
      fileUpload: {
        // 是否显示弹出层
        open: false,
        // 弹出层标题（
        title: "合同上传",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的数据
        contractId: "",
        // 设置上传的请求头部
        headers: {Authorization: "Bearer " + getToken()},
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "customer/contract/handleFileUpload"
      },

      isShowCustomerDrawer: false,
      isShowProductDrawer: false
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询合同管理列表 */
    getList() {
      this.loading = true;
      listContract(this.queryParams).then(response => {
        this.contractList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.updateopen = false;
      this.addopen = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        contractCode: null,
        contractName: null,
        contractStartDate: null,
        contractEndDate: null,
        contractAmount: null,
        companyId: null,
        companyCode: null,
        companyName: null,
        companyStatus: null,
        companyUserName: null,
        companyTitle: null,
        mobile: null,
        status: null,
        contractStatus: null,
        productId: null,
        productCode: null,
        productName: null,
        contractPicture: null,
        contractAccessory: null,
        ext: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.addopen = true;
      this.title = "新增合同";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getContract(id).then(response => {
        this.form = response.data;
        this.updateopen = true;
        this.title = "修改合同";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateContract(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.updateopen = false;
              this.getList();
            });
          } else {
            addContract(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.addopen = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除合同管理编号为"' + ids + '"的数据项？').then(function () {
        return delContract(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 详情按钮操作 */
    handleView(row) {
      this.detailopen = true;
      this.form = row;
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('customer/contract/export', {
        ...this.queryParams
      }, `contract_${new Date().getTime()}.xlsx`)
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download('customer/contract/importTemplate', {}, `customer_contract_template.xlsx`)
    },
    // 文件上传中处理
    handleUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleUploadSuccess(response, file, fileList) {
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", {dangerouslyUseHTMLString: true});
      this.getList();
    },
    // 提交上传文件
    submitUploadForm() {
      this.$refs.upload.submit();
    },
    showCustomerDrawer() {
      this.isShowCustomerDrawer = true;
    },
    showProductDrawer() {
      this.isShowProductDrawer = true;
    },
    hidenCustomerDrawer() {
      this.isShowCustomerDrawer = false;
    },
    hidenProductDrawer() {
      this.isShowProductDrawer = false;
    },
    confirmCustomer(customer) {
      // console.log("customer", customer);
      // console.log('form', this.form);
      const {id, companyCode, companyName, followStatus, userName, userTitle, phone} = customer;
      this.form.companyId = id;
      this.form.companyCode = companyCode;
      this.form.companyName = companyName;
      this.form.companyStatus = followStatus;
      this.form.companyUserName = userName;
      this.form.companyTitle = userTitle;
      this.form.mobile = phone;

      this.isShowCustomerDrawer = false; // 关闭抽屉
      // this.form.companyTitle = userTitle;
    },
    confirmProduct(product) {
      const {id, productCode, productName} = product;

      this.form.productId = id;
      this.form.productCode = productCode;
      this.form.productName = productName;

      this.isShowProductDrawer = false; // 关闭抽屉
    },


    /** 合同下载 */
    handleFileExport(row) {
      this.queryParams.id = row.id;
      this.queryParams.contractPicture = row.contractPicture;
      var filename = `contract_file_${new Date().getTime()}.pdf`
      this.download('customer/contract/fileExport', {
        ...this.queryParams
      }, filename)
    },
    /** 合同上传按钮操作 */
    handleFileUpload(row) {
      this.fileUpload.open = "合同上传";
      this.fileUpload.open = true;
      this.fileUpload.contractId = row.id
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.fileUpload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      this.fileUpload.open = false;
      this.fileUpload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", {dangerouslyUseHTMLString: true});
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    },




    /** 校验手机格式 */
    validateMobile(rule, value, callback){
      const mobileRegex = /^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/;
      if(!mobileRegex.test(value)){
        return callback(new Error('手机号格式不正确!'));
      }else{
        callback();
      }
    },
    /** 校验金额 */
    validateMoney(rule, value, callback){
      const regex = /^[1-9]\d*|^[1-9]\d*.\d+[1-9]$/;
      if(!regex.test(value)){
        return callback(new Error('金额填写格式不正确!'));
      }else{
        callback();
      }
    }




  }
};
</script>

<style scoped lang="scss">
.paddingDistance {
  padding-left: 150px;
}
</style>
